package com.webchat.ugc.messaegqueue.consumer.redis;

import com.webchat.common.enums.messagequeue.MessageQueueEnum;
import com.webchat.common.service.messagequeue.consumer.AbstractRedisDelayQueueConsumer;
import com.webchat.common.service.messagequeue.producer.MessageQueueProducer;
import com.webchat.common.util.JsonUtil;
import com.webchat.domain.dto.queue.MallOrderTimeoutMessageDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;


@Service
public class MallProductOrderTimeoutDelayRedisMQConsumer extends AbstractRedisDelayQueueConsumer<MallOrderTimeoutMessageDTO> {


    @Autowired
    private MessageQueueProducer<MallOrderTimeoutMessageDTO, Integer> messageQueueProducer;

    @Override
    protected MallOrderTimeoutMessageDTO convert(String s) {

        return JsonUtil.fromJson(s, MallOrderTimeoutMessageDTO.class);
    }

    @Override
    protected MessageQueueEnum getMessageQueue() {

        return MessageQueueEnum.QUEUE_MALL_ORDER_TIMEOUT_DELAY_MESSAGE;
    }

    @Override
    protected void doNextConsume(Set<String> messages) {

        /**
         * 双队列实现：延迟队列 + 分布式普通队列
         *
         * 延迟队列消费到一批任务，丢入普通队列排队消费
         * ps:
         * 当前是一个商城订单支付超时处理任务，需要考虑秒杀、
         * 大流量等场景下同一时刻需要处理大量订单数据，支持服务消峰，降低服务端处理压力
         */
        List<MallOrderTimeoutMessageDTO> messageDTOList = messages.stream().map(this::convert).toList();
        messageDTOList.forEach(mess ->
                messageQueueProducer.send(MessageQueueEnum.QUEUE_MALL_ORDER_TIMEOUT_MESSAGE, mess));
    }
}
